-- Tile
local Tile = {class = "Tile"}
Tile.__index = Tile

function Tile.init(o)
	return setmetatable(o, Tile)
end

function Tile:draw(x, y, r, sx, sy, ox, oy)
	love.graphics.draw(self.tileset.image, self.quad, self.tileset.offsetX + x, 
		self.tileset.offsetY + y, r, sx, sy, ox, oy)
end

local TileSet = {class = "TileSet"}
TileSet.__index = TileSet


function TileSet.init(ts)
	return setmetatable(ts, TileSet)
end

function TileSet:getTiles()
	local tiles = {}
	local id = self.firstgid
	local width, height = self.width, self.height
	local margin = self.margin
	local spacing = self.spacing
	local tilewidth, tileheight = self.tileWidth, self.tileHeight
	local advanceX, advanceY = tilewidth + spacing, self.tileHeight + spacing
	local endX = width  - margin - tilewidth
	local endY = height - margin - tileheight
	local types, properties = self.tileTypes, self.tileProperties
	for y = margin, endY, advanceY do
		for x = margin, endX, advanceX do
			tiles[id] = Tile.init{
				id = id,
				type = types[id],
				width = tilewidth,
				height = tileheight,
				properties = properties[id],
				tileset = self,
				quad = love.graphics.newQuad(x, y, tilewidth, tileheight, width, height),
			}
			id = id + 1
		end
	end
	
	return tiles
end

return TileSet
